package com.retail.proxy.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.retail.core.entity.enums.DelFlagEnum;
import com.retail.core.entity.enums.StatusFlagEnum;
import com.retail.proxy.entity.request.AdminProxyUpgradeDetailRequest;
import com.retail.proxy.entity.vo.ProxyMechanismCategoryVo;
import com.retail.proxy.entity.vo.ProxyUpgradeConfigVo;
import com.retail.proxy.entity.vo.SysAdminProxyUpgradeVo;
import com.retail.core.entity.ParamReq;
import com.retail.proxy.entity.request.SysAdminProxyUpgradeRequest;
import com.retail.proxy.mapper.AdminProxyUpgradeDetailMapper;
import com.retail.proxy.mapper.ProxyMechanismCategoryMapper;
import com.retail.proxy.mapper.ProxyUpgradeConfigMapper;
import com.retail.proxy.mapper.SysAdminProxyUpgradeMapper;
import com.retail.proxy.service.SysAdminProxyUpgradeService;
import com.retail.system.entity.vo.AdminVo;
import com.retail.system.mapper.AdminMapper;
import com.retail.team.enums.VerifyFlagEnums;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 代理晋升 Service impl
 *
 * @author Administrator
 */
@Service
public class SysAdminProxyUpgradeServiceImpl implements SysAdminProxyUpgradeService {

    @Resource
    private SysAdminProxyUpgradeMapper sysAdminProxyUpgradeMapper;

    @Resource
    private ProxyMechanismCategoryMapper proxyMechanismCategoryMapper;

    @Resource
    private AdminMapper adminMapper;

    @Resource
    private ProxyUpgradeConfigMapper proxyUpgradeConfigMapper;

    @Resource
    private AdminProxyUpgradeDetailMapper adminProxyUpgradeDetailMapper;


    @Override
    public SysAdminProxyUpgradeVo getModel(Long id) {
        SysAdminProxyUpgradeVo vo = sysAdminProxyUpgradeMapper.getModel(id);
        vo.setVerifyFlagName(VerifyFlagEnums.getStatusValues(vo.getVerifyFlag()));

        if (ObjectUtil.isNotEmpty(vo.getOraCategoryId())) {
            ProxyMechanismCategoryVo oraCategory = proxyMechanismCategoryMapper.getModel(vo.getOraCategoryId());
            if (ObjectUtil.isNotEmpty(oraCategory)) {
                vo.setOraCategoryIdName(oraCategory.getCategoryName());
            }
        }
        if (ObjectUtil.isNotEmpty(vo.getCategoryId())) {
            ProxyMechanismCategoryVo category = proxyMechanismCategoryMapper.getModel(vo.getCategoryId());
            if (ObjectUtil.isNotEmpty(category)) {
                vo.setCategoryIdName(category.getCategoryName());
            }
        }

        if (ObjectUtil.isNotEmpty(vo.getAdminId())) {
            AdminVo adminVo = adminMapper.getModel(vo.getAdminId());
            if (ObjectUtil.isNotEmpty(adminVo)) {
                vo.setAdminName(adminVo.getName());
            }
        }

        return vo;
    }

    @Transactional
    @Override
    public Long save(SysAdminProxyUpgradeRequest sysAdminProxyUpgradeRequest) {
        sysAdminProxyUpgradeMapper.save(sysAdminProxyUpgradeRequest);
        return sysAdminProxyUpgradeRequest.getId();
    }

    @Transactional
    @Override
    public Long update(SysAdminProxyUpgradeRequest request) {
        sysAdminProxyUpgradeMapper.update(request);
        if (ObjectUtil.equal(request.getVerifyFlag(), VerifyFlagEnums.SHEN_HE_TONGGUO.getValue())) {
            adminMapper.updateAdminLevelByAdminId(request.getAdminId(), request.getCategoryId());
            saveUpgradeLog(request.getAdminId());
        }
        return request.getId();
    }

    /**
     * 晋升等级
     *
     * @param adminId
     */
    private void saveUpgradeLog(Long adminId) {
        AdminVo currentUser = adminMapper.getModel(adminId);
        AdminVo parentUser = null;
        if (ObjectUtil.isNotEmpty(currentUser.getParentId())) {
            parentUser = adminMapper.getModel(currentUser.getParentId());
            List<AdminVo> childAdmins = adminMapper.getModelByParentAndAdminLevel(currentUser.getParentId(), currentUser.getAdminLevel());

            ProxyUpgradeConfigVo upgradeConfigVo = proxyUpgradeConfigMapper.getModelByOraCategoryId(currentUser.getAdminLevel());
            if (ObjectUtil.isNotEmpty(childAdmins) && ObjectUtil.isNotEmpty(upgradeConfigVo)) {
                if (childAdmins.size() >= upgradeConfigVo.getUpgradeCount() && parentUser.getAdminLevel() < upgradeConfigVo.getCategoryId()) {
                    SysAdminProxyUpgradeRequest sysAdminProxyUpgradeRequest = new SysAdminProxyUpgradeRequest();
                    sysAdminProxyUpgradeRequest.setAdminId(parentUser.getId());
                    sysAdminProxyUpgradeRequest.setOraCategoryId(parentUser.getAdminLevel());
                    sysAdminProxyUpgradeRequest.setCategoryId(upgradeConfigVo.getCategoryId());
                    sysAdminProxyUpgradeRequest.setVerifyFlag(VerifyFlagEnums.DAI_SHEN_HE.getValue());
                    sysAdminProxyUpgradeRequest.setStatusFlag(StatusFlagEnum.USE.getValue());
                    sysAdminProxyUpgradeRequest.setDelFlag(DelFlagEnum.NO.getValue());

                    sysAdminProxyUpgradeMapper.save(sysAdminProxyUpgradeRequest);

                    Long upgradeId = sysAdminProxyUpgradeRequest.getId();
                    childAdmins.forEach(cadmin -> {
                        AdminProxyUpgradeDetailRequest adminProxyUpgradeDetailRequest = new AdminProxyUpgradeDetailRequest();
                        adminProxyUpgradeDetailRequest.setAdminId(cadmin.getId());
                        adminProxyUpgradeDetailRequest.setUpgradeId(upgradeId);
                        adminProxyUpgradeDetailRequest.setCategoryId(cadmin.getAdminLevel());
                        adminProxyUpgradeDetailRequest.setStatusFlag(StatusFlagEnum.USE.getValue());
                        adminProxyUpgradeDetailRequest.setDelFlag(DelFlagEnum.NO.getValue());
                        adminProxyUpgradeDetailMapper.save(adminProxyUpgradeDetailRequest);
                    });
                }
            }
        }
    }


    @Override
    public List<SysAdminProxyUpgradeVo> listPage(ParamReq<SysAdminProxyUpgradeRequest> params) {
        params.setStartIndex((params.getCurrent() - 1) * params.getSize());
        List<SysAdminProxyUpgradeVo> res = sysAdminProxyUpgradeMapper.listPage(params);
        res.forEach(vo -> {
            vo.setVerifyFlagName(VerifyFlagEnums.getStatusValues(vo.getVerifyFlag()));

            if (ObjectUtil.isNotEmpty(vo.getOraCategoryId())) {
                ProxyMechanismCategoryVo oraCategory = proxyMechanismCategoryMapper.getModel(vo.getOraCategoryId());
                if (ObjectUtil.isNotEmpty(oraCategory)) {
                    vo.setOraCategoryIdName(oraCategory.getCategoryName());
                }
            }
            if (ObjectUtil.isNotEmpty(vo.getCategoryId())) {
                ProxyMechanismCategoryVo category = proxyMechanismCategoryMapper.getModel(vo.getCategoryId());
                if (ObjectUtil.isNotEmpty(category)) {
                    vo.setCategoryIdName(category.getCategoryName());
                }
            }

            if (ObjectUtil.isNotEmpty(vo.getAdminId())) {
                AdminVo adminVo = adminMapper.getModel(vo.getAdminId());
                if (ObjectUtil.isNotEmpty(adminVo)) {
                    vo.setAdminName(adminVo.getName());
                }
            }
        });

        return res;
    }

    @Override
    public Integer countPage(ParamReq<SysAdminProxyUpgradeRequest> params) {
        return sysAdminProxyUpgradeMapper.countPage(params);
    }

    @Override
    public Integer updateDelFlagById(Long id, Integer delFlag) {
        return sysAdminProxyUpgradeMapper.updateDelFlagById(id, delFlag);
    }
}
